package cn.bupt.adt.controller.inf;

import cn.bupt.adt.entity.autogenerate.*;
import cn.bupt.adt.model.CheckInstitutionModel;
import cn.bupt.adt.service.IInstitutionAppUserService;
import cn.bupt.adt.service.IinspectOrderService;
import cn.bupt.adt.service.impl.CheckInstitutionService;
import cn.bupt.adt.util.NetDataAccessUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/interface/inspection/Order")
public class inspectOrderinfController {

    @Autowired
    IinspectOrderService inspectOrderService;

    @Autowired
    IInstitutionAppUserService institutionAppUserService;

    @Autowired
    CheckInstitutionService checkInstitutionService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true:允许输入空值，false:不能为空值
    }

    //检查机构修改订单状态
    //更改订单状态,变1为接单、变2为取回样品、变3为检验完成
    @RequestMapping("/takeOrder")
    @ResponseBody
    public Object takeOrder(HttpServletRequest request, InspectOrder order) {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        try {
            Integer userId = (Integer) request.getAttribute("userId");
            CheckInstitutionModel ins = checkInstitutionService.getInstitutionById(userId);
            boolean rst = inspectOrderService.takeOrder(order, ins.getId());
            if (rst) {
                nau.setResult(1);
                nau.setResultDesp("接单成功");
            } else {
                nau.setResult(0);
                nau.setResultDesp("接单失败");
            }
        } catch (Exception e) {
            nau.setResult(0);
            nau.setResultDesp("接单失败");
        }

        return nau;
    }

    @RequestMapping("/changeOrderStatus")
    @ResponseBody
    public Object changeOrderStatus(HttpServletRequest request, InspectOrderView order) {
        boolean rst = inspectOrderService.updateOrder(order);
        NetDataAccessUtil nau = new NetDataAccessUtil();
        if (rst) {
            nau.setResult(1);
            nau.setResultDesp("修改检查项目订单成功");
        } else {
            nau.setResult(0);
            nau.setResultDesp("修改检查项目订单失败");
        }
        return nau;
    }


    //获取一级检查项目名称
    @RequestMapping("/getFirstLevelInspect")
    @ResponseBody
    public Object getFirstLevel(HttpServletRequest request) {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        List<FirstLevelInspectionItem> firstLevelInspectionItem = inspectOrderService.getAllFirstLevelInspectionItem();
        if (firstLevelInspectionItem.size() != 0) {
            nau.setContent(firstLevelInspectionItem);
            nau.setResult(1);
            nau.setResultDesp("获取一级检查项目成功");
        } else {
            nau.setContent(null);
            nau.setResult(0);
            nau.setResultDesp("获取一级检查项目失败");
        }
        return nau;
    }

    //获取二级检查项目名称
    @RequestMapping("/getSecondLevelInspect")
    @ResponseBody
    public Object getSecondLevel(HttpServletRequest request, Integer firstLevelId) {
        List<SecondLevelView> secondLevelView = inspectOrderService.getSecondLevelByfirstLevelId(firstLevelId);
        NetDataAccessUtil nau = new NetDataAccessUtil();
        if (secondLevelView.size() != 0) {
            nau.setContent(secondLevelView);
            nau.setResult(1);
            nau.setResultDesp("获取二级检查项目成功");
        } else {
            nau.setContent(null);
            nau.setResult(0);
            nau.setResultDesp("获取二级检查项目失败");
        }
        return nau;
    }

    //获取检查项目订单列表
    @RequestMapping("/getInspectItemsOrders")
    @ResponseBody
    public Object getInspectItems(HttpServletRequest request, Integer pageSize, Integer pageNum) {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        if (pageSize == null) {
            pageSize = 10;
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        Page<?> page = PageHelper.startPage(pageNum, pageSize, "id desc");
        List<InspectOrder> inspectOrder = inspectOrderService.getAllInspectItemOrdersInInspectOrder();

        if (inspectOrder.size() != 0) {
            Map<String, Object> content = new HashMap<>();
            content.put("orders", inspectOrder);
            content.put("currentPage", page.getPageNum());
            content.put("allPages", page.getPages());
            nau.setContent(content);
            nau.setResult(1);
            nau.setResultDesp("获取检查项目订单列表成功");
        } else {
            nau.setContent(null);
            nau.setResult(0);
            nau.setResultDesp("获取检查项目订单列表失败");
        }
        return nau;
    }

    //获取检查项目列表
    @RequestMapping("/getInspectItemsBySecondLevel")
    @ResponseBody
    public Object getInspects(HttpServletRequest request, String secondLevelId) {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        List<InspectView> inspectView = inspectOrderService.getInspectItemsBySecondLevel(secondLevelId);
        if (inspectView.size() != 0) {
            nau.setContent(inspectView);
            nau.setResult(1);
            nau.setResultDesp("获取检查项目列表成功");
        } else {
            nau.setContent(null);
            nau.setResult(0);
            nau.setResultDesp("获取检查项目列表失败");
        }
        return nau;
    }

    //通过firstLevel获取检查项目列表
    @RequestMapping("/getInspectOrderByFirstLevel")
    @ResponseBody
    public Object getInspectOrderByFirstLevel(HttpServletRequest request, String firstLevelId) {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        List<InspectView> inspectView = inspectOrderService.getInspectItemsByFirstLevel(firstLevelId);

        if (inspectView.size() != 0) {
            nau.setContent(inspectView);
            nau.setResult(1);
            nau.setResultDesp("获取检查项目列表成功");
        } else {
            nau.setContent(null);
            nau.setResult(0);
            nau.setResultDesp("获取检查项目列表失败");
        }
        return nau;
    }
}
